A major reworking of [[Special:Allmessages]].
[lhc/web/wiklou.git] / includes / SpecialAllpages.php
1 <?php
2
3 function wfSpecialAllpages( $par=NULL )
4 {
5 global $indexMaxperpage, $toplevelMaxperpage, $wgRequest, $wgOut, $wgLang;
6 $indexMaxperpage = 480;
7 $toplevelMaxperpage = 50;
8 $from = $wgRequest->getVal( 'from' );
9 $namespace = $wgRequest->getVal( 'namespace' );
10 if ( is_null($namespace) ) { $namespace = 0; }
11 $arr = $wgLang->getNamespaces();
12 $wgOut->setPagetitle ( $namespace > 0 ? wfMsg ( 'allpagesnamespace', $arr[$namespace] )
13 : wfMsg ( 'allarticles' ) );
14
15 if ( $par ) {
16 indexShowChunk( $par, $namespace );
17 } elseif ( $from ) {
18 indexShowChunk( $from, $namespace );
19 } else {
20 indexShowToplevel ( $namespace );
21 }
22 }
23
24 function namespaceForm ( $namespace = 0, $from = "" )
25 {
26 global $wgLang, $wgScript;
27
28 $t = Title::makeTitle( NS_SPECIAL, "Allpages" );
29
30 $namespaceselect = '<select name="namespace">';
31 $arr = $wgLang->getNamespaces();
32 for ( $i = 0; $i < 14; $i++ ) {
33 $namespacename = str_replace ( "_", " ", $arr[$i] );
34 $n = ($i == 0) ? wfMsg ( 'articlenamespace' ) : $namespacename;
35 $sel = ($i == $namespace) ? ' selected="selected"' : '';
36 $namespaceselect .= "<option value='{$i}'{$sel}>{$n}</option>";
37 }
38 $namespaceselect .= '</select>';
39
40 $frombox = '<input type="text" size="20" name="from" value="'
41 . htmlspecialchars ( $from ) . '"/>';
42 $submitbutton = '<input type="submit" value="' . wfMsg( 'go' ) . '" />';
43
44 $out = "<div class='namespaceselector'><form method='get' action='{$wgScript}'>";
45 $out .= '<input type="hidden" name="title" value="'.$t->getPrefixedText().'" />';
46 $out .= wfMsg ( 'allpagesformtext', $frombox, $namespaceselect, $submitbutton );
47 $out .= '</form></div>';
48 return $out;
49 }
50
51 function indexShowToplevel ( $namespace = 0 )
52 {
53 global $wgOut, $indexMaxperpage, $toplevelMaxperpage, $wgLang, $wgRequest, $wgUser;
54 $sk = $wgUser->getSkin();
55 $fname = "indexShowToplevel";
56 $namespace = intval ($namespace);
57
58 # Cache
59 $vsp = $wgLang->getValidSpecialPages();
60 $log = new LogPage( $vsp["Allpages"] );
61 $log->mUpdateRecentChanges = false;
62
63 global $wgMiserMode;
64 if ( $wgMiserMode ) {
65 $log->showAsDisabledPage();
66 return;
67 }
68
69 $dbr =& wfGetDB( DB_SLAVE );
70 $cur = $dbr->tableName( 'cur' );
71 $fromwhere = "FROM $cur WHERE cur_namespace=$namespace";
72 $order_arr = array ( 'ORDER BY' => 'cur_title' );
73 $order_str = 'ORDER BY cur_title';
74 $out = "";
75 $where = array( 'cur_namespace' => $namespace );
76
77 $count = $dbr->selectField( 'cur', 'COUNT(*)', $where, $fname );
78 $sections = ceil( $count / $indexMaxperpage );
79
80 # We want to display $toplevelMaxperpage lines starting at $offset.
81 # NOTICE: $offset starts at 0
82 $offset = intval ( $wgRequest->getVal( 'offset' ) );
83 if ( $offset < 0 ) { $offset = 0; }
84 if ( $offset >= $sections ) { $offset = $sections - 1; }
85
86 # Where to stop? Notice that this can take the value of $sections, but $offset can't, because if
87 # we're displaying only the very last section, we still need two DB queries to find the titles
88 $stopat = ( $offset + $toplevelMaxperpage < $sections )
89 ? $offset + $toplevelMaxperpage : $sections ;
90
91 # This array is going to hold the cur_titles in order.
92 $lines = array();
93
94 # If we are going to show n rows, we need n+1 queries to find the relevant titles.
95 for ( $i = $offset; $i <= $stopat; $i++ ) {
96 if ( $i == $sections ) # if we're displaying the last section, we need to
97 $from = $count-1; # find the last cur_title in the DB
98 else if ( $i > $offset )
99 $from = $i * $indexMaxperpage - 1;
100 else
101 $from = $i * $indexMaxperpage;
102 $limit = ( $i == $offset || $i == $stopat ) ? 1 : 2;
103 $sql = "SELECT cur_title $fromwhere $order_str " . $dbr->limitResult ( $limit, $from );
104 echo "($offset/$stopat) $sql<br>\n";
105 $res = $dbr->query( $sql, $fname );
106 $s = $dbr->fetchObject( $res );
107 array_push ( $lines, $s->cur_title );
108 if ( $s = $dbr->fetchObject( $res ) ) {
109 array_push ( $lines, $s->cur_title );
110 }
111 $dbr->freeResult( $res );
112 }
113
114 # At this point, $lines should contain an even number of elements.
115 $out .= "<table style='background: inherit;'>";
116 while ( count ( $lines ) > 0 ) {
117 $inpoint = array_shift ( $lines );
118 $outpoint = array_shift ( $lines );
119 $out .= indexShowline ( $inpoint, $outpoint, $namespace );
120 }
121 $out .= "</table>";
122
123 $nsForm = namespaceForm ( $namespace );
124
125 # Is there more?
126 $morelinks = "";
127 if ( $offset > 0 ) {
128 $morelinks = $sk->makeKnownLink (
129 $wgLang->specialPage ( "Allpages" ),
130 wfMsg ( 'allpagesprev' ),
131 ( $offset > $toplevelMaxperpage ) ? 'offset='.($offset-$toplevelMaxperpage) : ''
132 );
133 }
134 if ( $stopat < $sections-1 ) {
135 if ( $morelinks != "" ) { $morelinks .= " | "; }
136 $morelinks .= $sk->makeKnownLink (
137 $wgLang->specialPage ( "Allpages" ),
138 wfMsg ( 'allpagesnext' ),
139 'offset=' . ($offset + $toplevelMaxperpage)
140 );
141 }
142
143 if ( $morelinks != "" ) {
144 $out2 = '<table style="background: inherit;" width="100%" cellpadding="0" cellspacing="0" border="0">';
145 $out2 .= '<tr valign="top"><td align="left">' . $nsForm;
146 $out2 .= '</td><td align="right" style="font-size: smaller; margin-bottom: 1em;">';
147 $out2 .= $morelinks . '</td></tr></table><hr />';
148 } else {
149 $out2 = $nsForm . '<hr />';
150 }
151
152 # Saving cache
153 $log->replaceContent( $out );
154
155 $wgOut->addHtml( $out2 . $out );
156 }
157
158 function indexShowline( $inpoint, $outpoint, $namespace = 0 )
159 {
160 global $wgOut, $wgLang, $wgUser;
161 $sk = $wgUser->getSkin();
162 $dbr =& wfGetDB( DB_SLAVE );
163
164 $inpointf = str_replace( "_", " ", $inpoint );
165 $outpointf = str_replace( "_", " ", $outpoint );
166 $queryparams = 'from=' . $dbr->strencode( $inpoint );
167 if ( $namespace > 0 ) { $queryparams .= '&namespace='.intval($namespace); }
168 $out = wfMsg(
169 'alphaindexline',
170 $sk->makeKnownLink( $wgLang->specialPage( "Allpages" ), $inpointf, $queryparams ) . '</td><td>',
171 '</td><td align="left">' . $outpointf
172 );
173 return '<tr><td align="right">'.$out.'</td></tr>';
174 }
175
176 function indexShowChunk( $from, $namespace = 0 )
177 {
178 global $wgOut, $wgUser, $indexMaxperpage, $wgLang;
179 $sk = $wgUser->getSkin();
180 $maxPlusOne = $indexMaxperpage + 1;
181 $namespacee = intval($namespace);
182
183 $out = "";
184 $dbr =& wfGetDB( DB_SLAVE );
185 $cur = $dbr->tableName( 'cur' );
186 $sql = "SELECT cur_title FROM $cur WHERE cur_namespace=$namespacee AND cur_title >= '"
187 . $dbr->strencode( $from ) . "' ORDER BY cur_title LIMIT " . $maxPlusOne;
188 $res = $dbr->query( $sql, "indexShowChunk" );
189
190 ### FIXME: side link to previous
191
192 $n = 0;
193 $out = '<table style="background: inherit;" border="0" width="100%">';
194 while( ($n < $indexMaxperpage) && ($s = $dbr->fetchObject( $res )) ) {
195 $t = Title::makeTitle( $namespacee, $s->cur_title );
196 if( $t ) {
197 $link = $sk->makeKnownLinkObj( $t, $t->getText() );
198 } else {
199 $link = '[[' . htmlspecialchars( $s->cur_title ) . ']]';
200 }
201 if( $n % 3 == 0 ) {
202 $out .= '<tr>';
203 }
204 $out .= "<td>$link</td>";
205 $n++;
206 if( $n % 3 == 0 ) {
207 $out .= '</tr>';
208 }
209 }
210 if( ($n % 3) != 0 ) {
211 $out .= '</tr>';
212 }
213 $out .= '</table>';
214
215 $nsForm = namespaceForm ( $namespace, $from );
216 $out2 = '<table style="background: inherit;" width="100%" cellpadding="0" cellspacing="0" border="0">';
217 $out2 .= '<tr valign="top"><td align="left">' . $nsForm;
218 $out2 .= '</td><td align="right" style="font-size: smaller; margin-bottom: 1em;">' .
219 $sk->makeKnownLink( $wgLang->specialPage( "Allpages" ),
220 wfMsg ( 'allpages' ) );
221 if ( ($n == $indexMaxperpage) && ($s = $dbr->fetchObject( $res )) ) {
222 $out2 .= " | " . $sk->makeKnownLink(
223 $wgLang->specialPage( "Allpages" ),
224 wfMsg ( 'nextpage', $s->cur_title ),
225 "from=" . wfUrlEncode ( $s->cur_title ) );
226 }
227 $out2 .= "</td></tr></table><hr />";
228
229 $wgOut->addHtml( $out2 . $out );
230 }
231
232 ?>